﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

public partial class CountryMaster: System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                clsCity clsCity = new clsCity();
               FillCountry();
//FillddlState();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
    protected void grdCountary_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdCountary.PageIndex = e.NewPageIndex;
        FillCountry();
    }
    protected void btnCountry_Click(object sender, EventArgs e)
    {



        try
        {
            if (Session["CountryId"] == null)
            {
                clsCity objclsCity = new clsCity();
                IN_TB_COUNTRY objIN_TB_COUNTRY = new IN_TB_COUNTRY();
                objIN_TB_COUNTRY.COUNTRY  = txtCountryName.Text;

                int countryid = objclsCity.SaveCountryMst(objIN_TB_COUNTRY);
                ClearAllControls();
               FillCountry();
            }
            else
            {
                clsCity objclsCity = new clsCity();
                IN_TB_COUNTRY objIN_TB_COUNTRY = new IN_TB_COUNTRY();
                objIN_TB_COUNTRY.ID = Convert.ToInt32(Session["CountryId"].ToString());
                objIN_TB_COUNTRY.COUNTRY = txtCountryName.Text;
                int countryid = objclsCity.SaveCountryMst(objIN_TB_COUNTRY);
                FillCountry();
                ClearAllControls();
                Session["CountryId"] = null;
            }
        }

        catch (Exception)
        {
            throw;
        }
    }

    protected void ClearAllControls()
    {
        try
        {
            txtCountryName.Text = string.Empty;
                   }
        catch (Exception)
        {

            throw;
        }
    }

    protected void FillCountry()
    {
        try
        {
            List<IN_TB_COUNTRY> objIN_TB_COUNTRYist = new List<IN_TB_COUNTRY>();
            clsCity objIN_TB_COUNTRYDAL = new clsCity();
            objIN_TB_COUNTRYist = objIN_TB_COUNTRYDAL.GetAllCountry ();
            if (objIN_TB_COUNTRYist != null)
            {
                grdCountary.DataSource = objIN_TB_COUNTRYist;
                grdCountary.DataBind();
            }
        }
        catch (Exception)
        {

            throw;
        }
    }
    protected void grdCountary_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Edt")
            {
                int CountryId = Convert.ToInt32(e.CommandArgument.ToString());
                clsCity objIN_TB_COUNTRYDAL = new clsCity();
                IN_TB_COUNTRY objIN_TB_COUNTRY = new IN_TB_COUNTRY();
                objIN_TB_COUNTRY = objIN_TB_COUNTRYDAL.GetCountryById(CountryId);
                if (objIN_TB_COUNTRY != null)
                {

                    txtCountryName.Text = objIN_TB_COUNTRY.COUNTRY;
                    Session["CountryId"] = CountryId;
                }
            }

            if (e.CommandName == "Del")
            {
                int CountryId = Convert.ToInt32(e.CommandArgument.ToString());
                clsCity objIN_TB_COUNTRYDAL = new clsCity();
                IN_TB_COUNTRY objIN_TB_COUNTRY = new IN_TB_COUNTRY();
                objIN_TB_COUNTRYDAL.DeleteCountry(CountryId);
                FillCountry();
            }
        }
        catch (Exception)
        {

            throw;
        }
    }
    protected void grdCountary_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        //try
        //{
        //    if (e.Row.RowType == DataControlRowType.DataRow)
        //    {
        //        //Getting State Names
        //        int CountryID = Convert.ToInt32(e.Row.Cells[o].Text);
        //        clsCity objclsCity = new clsCity();
        //        IN_TB_STATE objIN_TB_STATE = new IN_TB_STATE();
        //        objIN_TB_STATE = objclsCity.GetStateById(StateId);
        //        e.Row.Cells[1].Text = objIN_TB_STATE.STATE;

        //    }
        //}
        //catch (Exception)
        //{

        //    throw;
        //}

    }
    protected void grdCountary_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
